package com.extrace.ui.main;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

import androidx.annotation.RequiresApi;

import com.extrace.map.LocationService;
import com.extrace.misc.model.UserInfo;

public class ExTraceApplication extends Application {
    //private static final String PREFS_NAME = "ExTrace.cfg";
    SharedPreferences settings;// = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    //	String mServerUrl;
//	String mMiscService,mDomainService;
    UserInfo userInfo;

    public String getServerUrl() {
        return settings.getString("ServerUrl", "");
    }
    public String getMiscServiceUrl() {
        return getServerUrl() + settings.getString("MiscService", "");
    }
    public String getDomainServiceUrl() {
        return getServerUrl() + settings.getString("DomainService", "");
    }

    public void UserLogin(UserInfo ui){
        userInfo.setURull(ui.getURull());
        userInfo.setTelCode(ui.getTelCode());
        userInfo.setStatus(ui.getStatus());
        userInfo.setTransPackageID(ui.geTransPackageID());
        userInfo.setReceivePackageID(ui.getReceivePackageID());
        userInfo.setDptID(ui.getDptID());
        userInfo.setDelivePackageID(ui.getDelivePackageID());
        userInfo.setPWD(ui.getPWD());
        userInfo.setName(ui.getName());
        userInfo.setID(ui.getUID());
    }

    public UserInfo getLoginUser(){
        return userInfo;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onCreate() {
        super.onCreate();
        settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        //SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        //临时造一个用户
        startAlarm();
        userInfo = new UserInfo();
        userInfo.setID(1);
        /*userInfo.setURull(0);
        userInfo.setReceivePackageID("rp1");
        userInfo.setTransPackageID("tp1");
        userInfo.setDelivePackageID("dp1");*/
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    public void startAlarm(){
        /**
         首先获得系统服务
         */
        AlarmManager am = (AlarmManager)
                getSystemService(Context.ALARM_SERVICE);

        //设置闹钟的意图，我这里是去调用一个服务，该服务功能就是获取位置并且上传
        Intent intent = new Intent(this, LocationService.class);
        PendingIntent pendSender = PendingIntent.getService(this, 0, intent, 0);
        am.cancel(pendSender);

        //AlarmManager.RTC_WAKEUP 这个参数表示系统会唤醒进程；我设置的间隔时间是5秒
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5*1000, pendSender);

    }

    public void onTerminate() {
        super.onTerminate();

        //save data of the map
    }
}
